<template>
  <div>
    <template v-if="isBind === 0">
      <a-button @click="getCertificateInfo">获取证书信息</a-button>
      <a-button type="primary" style="margin-left:16px;" @click="bindCa">绑定证书信息</a-button></template
    >
    <template v-if="isBind === 1">
      CA证书已绑定
      <a-button type="primary" style="margin-left:16px;" @click="isBind = 0">修改绑定证书信息</a-button></template
    >

    <a-descriptions style="margin-top:16px;" title="证书信息" :column="1" bordered v-if="certInfo">
      <a-descriptions-item v-for="(value, key) in certInfo" :key="key" :label="key">
        {{ value }}
      </a-descriptions-item>
    </a-descriptions>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  props: {
    user: {
      type: Object,
    },
  },
  computed: { ...mapGetters(['userInfo']) },
  watch: {
    userInfo: {
      handler(val, oldval) {
        this.isBind = val.isCa
      },
      immediate: true,
    },
  },
  data() {
    return {
      isBind: null,
      provider: document.getElementById('provider').value,
      certInfo: null,
    }
  },
  methods: {
    bindCa() {
      let signData = _P7SignData()
      let serialNumber = GetDeviceSerialNumber()
      if (signData && serialNumber) {
        this.$http.post('/api/ca/caBind', { signData: signData, serialNumber: serialNumber }).then(res => {
          this.$message.success('绑定成功')
          this.$emit('update', this.form)
        })
      } else {
        this.$message.success('请检查CA证书')
      }
    },
    getCertificateInfo() {
      try {
        const certInfo = this.retrieveCertificateInfo(this.provider)
        this.certInfo = certInfo
        this.$message.success('证书信息获取成功')
      } catch (error) {
        this.$message.error(`获取证书信息失败: ${error.message}`)
      }
    },

    retrieveCertificateInfo(provider) {
      // 假设这些常量已经在全局定义
      // const SIGN_TYPE = 1 // 示例值，请根据实际情况调整
      // const X509_CERT_USCC = 2
      // const X509_CERT_COUNTRY = 3
      // const X509_CERT_PROVINCE = 4
      // const X509_CERT_CITY = 5
      // const X509_CERT_EMAIL = 6
      // const X509_CERT_APPLICATIONNAME = 7
      // const X509_CERT_ENTERPRISENAME = 8

      // 初始化
      if (typeof init !== 'function') {
        throw new Error('init function is not defined')
      }
      init(provider, '', '')

      // 获取证书
      if (typeof GetCert !== 'function') {
        throw new Error('GetCert function is not defined')
      }
      const signCert = GetCert(SIGN_TYPE)
      if (!signCert) {
        throw new Error('Failed to get certificate')
      }

      // 获取证书信息
      if (typeof GetCertInfo !== 'function') {
        throw new Error('GetCertInfo function is not defined')
      }

      return {
        '企业名称/姓名': GetCertInfo(signCert, X509_CERT_ENTERPRISENAME),
        统一社会信用代码: GetCertInfo(signCert, X509_CERT_USCC),
        省名称: GetCertInfo(signCert, X509_CERT_PROVINCE),
        市名称: GetCertInfo(signCert, X509_CERT_CITY),
        地区: GetCertInfo(signCert, X509_CERT_APPLICATIONNAME),
      }
    },
  },
}
</script>
